package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    //分页查询所有频道信息
    @Override
    public ResponseResult<AdChannel> findChannelByPage(ChannelDTO dto) {
        //1.参数校验 dto是否为空，分页参数是否正确
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //检查分页参数
        dto.checkParam();
        //分页查询
        Page pageReq = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(dto.getName()),AdChannel::getName,dto.getName());
        IPage<AdChannel> pageResult = this.page(pageReq, wrapper);
        //返回结果
        return new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal(),pageResult.getRecords());
    }

    /*
    * 添加新频道
    * */
    @Override
    public ResponseResult<AdChannel> insertAdChannel(AdChannel adChannel) {
        //参数校验，参数为不为空，频道名称不能为空，不能大于10个字符
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"参数不能为空");
        }
        if (StringUtils.isBlank(adChannel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能为空");
        }
        if (adChannel.getName().length()>10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称字符长度不能大于10");

        }
        //参数校验，频道名称是否重复，不重复则添加
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
        if (count>0){//频道名称已存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道以存在");
        }
        //所有校验已做，现在开始添加频道
        adChannel.setCreatedTime(new Date());//传递参数中没有创建时间
        boolean save = this.save(adChannel);
        if (save){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR,"添加频道失败");
    }

    /*
    * 频道修改，频道启用或是禁用
    * */
    @Override
    public ResponseResult<AdChannel> updateAdChannel(AdChannel adChannel) {
        //参数校验，无频道名称则不用处理，若有名称，检查名称是否改变，是否在数据库中已存在，长度是否符合
       if (adChannel == null || adChannel.getId()==null){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"无参数，或是没有参数id");
       }
       //判断该频道是否存在，存在名称是否唯一
        AdChannel channel = this.getById(adChannel.getId());
       if (channel == null){
           return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST,"频道不存在");

       }
        if (StringUtils.isNotBlank(adChannel.getName()) && !adChannel.getName().equals(channel.getName())){
            //判断新的频道名称在数据库是否存在
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
            if (count>0){//频道名称在数据库已存在
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道已存在");

            }
        }
        //名称存在，修改
        boolean b = this.updateById(adChannel);
        if (b){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR,"修改频道失败");

    }

    /*
    * 频道删除，启用状态不能删除
    * */
    @Override
    public ResponseResult<AdChannel> deleteAdChannel(Integer id) {
     //参数校验
        if (id ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"参数有误");
        }
        //判断在数据库中是否存在
        AdChannel adChannel = this.getById(id);
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST,"数据库不存在该频道");
        }
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_STATUS_TRUE,"频道启用不能删除");

        }
        //int i = 1/0;
        boolean b = this.removeById(id);
        if (b){
          return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"删除失败");
    }
}
